package cc.io24.knewcafe.boss.util;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;

import com.fasterxml.jackson.annotation.JsonInclude;

public class ResponseFormatter implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 7696366818798193754L;

    private String status = "SUCCESS";
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;
    private Object data;

    public static ResponseFormatter error(String status, String message) {
        ResponseFormatter rf = new ResponseFormatter();
        rf.setStatus(status);
        rf.setMessage(message);
        return rf;
    }

    public static ResponseFormatter singleFoo(Object foo) {
        ResponseFormatter rf = new ResponseFormatter();
        rf.data = foo;
        return rf;
    }

    public static ResponseFormatter fooList(List fooList) {
        ResponseFormatter rf = new ResponseFormatter();
        Data data = rf.new Data();
        data.setList(fooList);
        data.setTotal(fooList.size());
        data.setPageSize(100);
        data.setCurrentPage(1);

        rf.setData(data);
        return rf;
    }

    public static ResponseFormatter fooList(List fooList, int total, int currentPage, int pageSize) {
        ResponseFormatter rf = new ResponseFormatter();
        Data data = rf.new Data();
        data.setList(fooList);
        data.setTotal(total);
        data.setPageSize(pageSize);
        data.setCurrentPage(currentPage);
        rf.setData(data);
        return rf;
    }

    public static ResponseFormatter fooList(Page page) {
        ResponseFormatter rf = new ResponseFormatter();
        Data data = rf.new Data();
        data.setList(page.getContent());
        data.setTotal((int) page.getTotalElements());
        data.setPageSize(page.getSize());
        data.setCurrentPage(page.getNumber() + 1);
        rf.setData(data);
        return rf;
    }

    class Data {
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private int total;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private int pageSize;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private int currentPage;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private List list;

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getCurrentPage() {
            return currentPage;
        }

        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }

        public List getList() {
            return list;
        }

        public void setList(List list) {
            this.list = list;
        }

    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
        if (data == null) {
            this.status = "ERROR";
            this.message = "服务器未知异常，请联系管理员。";
        }
    }

}

